package com.secondshop.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.secondshop.models.Cart;
import com.secondshop.models.Good;
import com.secondshop.models.Order;
import com.secondshop.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.PublicKey;


public class Alipay {



    public String QRCode(Float price,String out_trade_no){
         String erweima=null;
         String APP_ID = "2021002146632297";
         String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4BcqOL8RNK3pyrR+LbxJ9Ipk2pyyhbOcXk/FF8TFuoRDnqPcjSjCINLXdo7G60McAA+nKkGALp+aLpLX7cLGX5TV7biyU7ZCQbCzp37gAl+Ylq/6mVgT5ByiArT2ax9v7o0LoECY3g9E/OHMgrGEtafKTVw8raYy6la2zDI6rcrMfihTJu+Kuf/Qa26bJVGQyAOhvrzaaZXe+2+UhyPPyxdqqB9weEOULlqwC8128dTv8gwuA5cZYbw8vkGKWxm/fbszjjdS28OdtbkoGE8cvDvWoNiNRlAZB6UT/mzAM5h2e8OYG31OvM9aiROMCPdlqavfUfbkOXomrH5XOqXbHwIDAQAB";
         String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUwD7xdovBi2Mb/xEtH2e0wmyQ9PG1AH1c26PafXt2EEzwZYIvtBTfm3ao6w7fIbbTKNwdL8LP/39XK5ThUjUobbQNWNK88mj/mUbppSXRfH61EuL912n3dDBhRcvo1d1yNsjzLZO8RtA6Hw3aNFZn4KgyyXt0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ICaI/rX95YY0YLzgdHKuu7OfTs0iJ9/cuwhzGqcGb7NINPyXnHWy3RCn1F1RD407eetg9+gGFsiJDnL9zGSBrMIM//64g2l2cjEroLpaWV5UfzoevY+nN+hbs77l/o67A2UTlloSdL5WDAx5yGzpkCgYEAkzkuGv7YeMt+R7WCgOWp96Eb0h8JGWqExlOoQt4hjnvsQ9QEvtE3NT5IEMqQCEn/QkjZhUlmjBsPEvk9rRzIpx2VM32ReKrWwT/H14I8MIDDNJ9Dv/ZWoyuksZFunbHcfEnhBdz0FYzj31KygVf/c9CWRibQryT+VWwGXnj4+nc=";
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");  //获得初始化的AlipayClient
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest (); //创建API对应的request类
        /** 设置业务参数  **/
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        /** 商户订单号，商户自定义，需保证在商户端不重复，如：20200612000001 **/
        model.setOutTradeNo(out_trade_no);

        /** 销售产品码，固定值：FACE_TO_FACE_PAYMENT **/
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        /**订单标题 **/
        model.setSubject("test");

        /** 订单金额，精确到小数点后两位 **/
        model.setTotalAmount("0.01");

        /** 订单描述 **/
        model.setBody("3AP");
        /** 将业务参数传至request中 **/
        request.setBizModel(model);

        /** 异步通知地址，以http或者https开头的，商户外网可以post访问的异步地址，用于接收支付宝返回的支付结果，如果未收到该通知可参考该文档进行确认：https://opensupport.alipay.com/support/helpcenter/193/201602475759 **/
        request.setNotifyUrl("https://www.baidu.com/");
        AlipayTradePrecreateResponse response = null;
        try {

            /** 通过alipayClient调用API，获得对应的response类  **/
            response = alipayClient.execute(request);

        } catch (AlipayApiException e) {

            e.printStackTrace();
        }
        System.out.print( response.getBody ());
        erweima= response.getQrCode();
        //根据response中的结果继续业务逻辑处理
        return erweima;
    }

    public String payQuery(String out_trade_no) {

        String APP_ID = "2021002146632297";
        String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4BcqOL8RNK3pyrR+LbxJ9Ipk2pyyhbOcXk/FF8TFuoRDnqPcjSjCINLXdo7G60McAA+nKkGALp+aLpLX7cLGX5TV7biyU7ZCQbCzp37gAl+Ylq/6mVgT5ByiArT2ax9v7o0LoECY3g9E/OHMgrGEtafKTVw8raYy6la2zDI6rcrMfihTJu+Kuf/Qa26bJVGQyAOhvrzaaZXe+2+UhyPPyxdqqB9weEOULlqwC8128dTv8gwuA5cZYbw8vkGKWxm/fbszjjdS28OdtbkoGE8cvDvWoNiNRlAZB6UT/mzAM5h2e8OYG31OvM9aiROMCPdlqavfUfbkOXomrH5XOqXbHwIDAQAB";
        String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUwD7xdovBi2Mb/xEtH2e0wmyQ9PG1AH1c26PafXt2EEzwZYIvtBTfm3ao6w7fIbbTKNwdL8LP/39XK5ThUjUobbQNWNK88mj/mUbppSXRfH61EuL912n3dDBhRcvo1d1yNsjzLZO8RtA6Hw3aNFZn4KgyyXt0miQmkokKyJExgQ5CHbAAYTWjWvvb5I57j27n8XPK/qFMljH4iQrzB2sUFy5FK1jANKBOJ8RS/3tvPuTUjgFNbcIlSrbS0EQj1YzOa8dSDRsJOhOSiAcRYY1zNcnsTK13DIpzvNWXf6DIdvgF2025QJoFBJYsSoHM6rgtrpisigFD24Pr2Qnwb/DTAgMBAAECggEAL2KSan8WBCCwAMIfKZ2gfq+UwBSXTs6KIOOC7d4dTz3gFJBxiVq3b4fpo+PFSAVIZTnV7KJDT9kXKqCie6ihcHCJ89qAv1oFYq7kLRfD1VjwUCRlFPHjOsXTh5khXNmMAo87MmQpdMemG227DvCvOcYSLqOKZVmY/ISfjUTZI8h43ynEnfzE2fwGw6F4FwltwzgvHLMqhTQIluE8FKRBrq/qyDISnfJHtebyrH+3IU7p1rTIYHbKOlGp6c0v6X/YFS3mwo/3kWcQPPmRL0s3SUck5QgKlklbWjIYRu3TgL4SeGGiK2QBHyvGTpreIQh7Sg+LcEB2EjSP+9RZ49TvSQKBgQDXdWa2PhMKiI5YWo8wftJYfYESThol4V0B1L/yM/0pnBVPOKtg873nWfZODTraYKGFfXEqvsb4235DMyJpCSMFreD208YCG2u4XwGDsagx7q8ap9rRYgQKx9Zjj9b2u64khWwOb6j8fA6nf3JxBf9+s6HTNhq22oIEgIyt0cQ9DQKBgQCwvYz63ZizWqaRPRQimEFHEItzr8DLzlIL2C2pBqXS06X77/rD0Y3KxM25xWCGNvHlB01PWaCV2Lq0jAJYvEGSmrYy/Zm7PY1lHvnpZd/zvF0D5kut+M5a9rHYvRct1pDYnyzEPCmzvWhlH9NPn12N4j6iz9ci2F1bqE+1/OotXwKBgEl6QjL4bGn1NAYxrA9v0LBY7MGfXQ96EcvkECm7TST5k9LWkquXtDXH2lW38VA0ECGELCnVsemAgUjNkXicoxL2v3ettSNW0p+n1s5gsemFarX+dINfl91NVXHSxg6S5sqRrc4mikzAygaZUh/1WbKN9CwhkW62igybAl7s4K2pAoGAQpboKwyan9ijsU0F7RLYZWICaI/rX95YY0YLzgdHKuu7OfTs0iJ9/cuwhzGqcGb7NINPyXnHWy3RCn1F1RD407eetg9+gGFsiJDnL9zGSBrMIM//64g2l2cjEroLpaWV5UfzoevY+nN+hbs77l/o67A2UTlloSdL5WDAx5yGzpkCgYEAkzkuGv7YeMt+R7WCgOWp96Eb0h8JGWqExlOoQt4hjnvsQ9QEvtE3NT5IEMqQCEn/QkjZhUlmjBsPEvk9rRzIpx2VM32ReKrWwT/H14I8MIDDNJ9Dv/ZWoyuksZFunbHcfEnhBdz0FYzj31KygVf/c9CWRibQryT+VWwGXnj4+nc=";
        String URL = "https://openapi.alipay.com/gateway.do";


        /** 初始化 **/
        AlipayClient alipayClient = new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,"json","UTF-8",ALIPAY_PUBLIC_KEY,"RSA2");

        /** 实例化具体API对应的request类，类名称和接口名称对应,当前调用接口名称：alipay.trade.query（统一收单线下交易查询） **/
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        /** 设置业务参数 **/
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();

        /** 注：交易号（TradeNo）与订单号（OutTradeNo）二选一传入即可，如果2个同时传入，则以交易号为准 **/
        /** 支付接口传入的商户订单号。如：2020061601290011200000140004 **/
        model.setOutTradeNo(out_trade_no);

        /** 异步通知/查询接口返回的支付宝交易号，如：2020061622001473951448314322 **/
        //model.setTradeNo("2020061622001473951448314322");

        /** 将业务参数传至request中 **/
        request.setBizModel(model);

        AlipayTradeQueryResponse response = null;
        try {

            /** 通过alipayClient调用API，获得对应的response类  **/
            response = alipayClient.execute(request);

        } catch (AlipayApiException e) {

            e.printStackTrace();
        }

        /** 获取接口调用结果，如果调用失败，可根据返回错误信息到该文档寻找排查方案：https://opensupport.alipay.com/support/helpcenter/101 **/
        System.out.println(response.getBody());
        String msg=response.getMsg();
        return msg;
    }






}
